<template>
  <transition name="slide">
    <music-list :rank="rank" :title="title" :bg-image="bgImage" :songs="songs"></music-list>
  </transition>
</template>

<script>
import MusicList from "../music-list/music-list";
import { mapMutations } from "vuex";
import { mapGetters } from "vuex";
import { getMusicList } from "../../api/rank";
import { ERR_OK } from "../../api/config";
import { createSong } from "../../common/js/song";
export default {
  components: {
    MusicList
  },
  created() {
    this._getTopList();
  },
  computed: {
    title() {
      return this.topList.topTitle;
    },
    bgImage() {
      return this.topList.picUrl;
    },
    ...mapGetters(["topList"])
  },
  data() {
    return {
      songs: [],
      rank: true
    };
  },
  methods: {
    _getTopList() {
      if (!this.topList.id) {
        this.$router.push("/rank");
      }

      getMusicList(this.topList.id).then(res => {
        if (res.code === ERR_OK) {
          this.songs = this._normalizeSongs(res.songlist);
        }
      });
    },
    _normalizeSongs(list) {
      let ret = [];
      list.forEach(item => {
        const musicData = item.data;
        if (musicData.songid && musicData.albumid) {
          ret.push(createSong(musicData));
        }
      });
      return ret;
    }
  }
};
</script>

<style lang="stylus" scoped>
.slide-enter-active, .slide-leave-active
  transition: all 0.3s ease
.slide-enter, .slide-leave-to
  transform: translate3d(100%, 0, 0)
</style>
